package com.mg.order.controller;

import com.mg.common.exception.ControllerException;
import com.mg.common.exception.MGExceptionEnum;
import com.mg.common.vo.ResponsePageResult;
import com.mg.common.vo.ResponseResult;
import com.mg.common.vo.ResponseStatus;
import com.mg.order.pojo.dto.RequestOrderBody;
import com.mg.order.pojo.vo.ResponseOrderVo;
import com.mg.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("genarate")
    public ResponseResult<Void> genarate(@Valid @RequestBody RequestOrderBody orderBody) {
        Long res = orderService.genarate(orderBody);
        if (res == null) {
            throw new ControllerException(MGExceptionEnum.UNKNOWN_ERROR);
        }
        return ResponseResult.ok(res);
    }

    @GetMapping("getpage")
    public ResponseResult<Void> getPage(@RequestParam(defaultValue = "0") Integer curr
            , @RequestParam(defaultValue = "5") Integer size,
                                        @RequestParam Long userId
    ) {
        //Todo 调用授权中心身份验证
        ResponsePageResult<List<ResponseOrderVo>> result = orderService.getPage(curr, size, userId);
        if (result == null) {
            ResponseResult.error(ResponseStatus.UNKNOWN_ERROR);
        }
        return ResponseResult.ok(result);
    }
}
